package com.support.base

import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.kongzue.dialogx.dialogs.*
import com.kongzue.dialogx.interfaces.OnBindView
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener
import com.kongzue.dialogx.interfaces.OnMenuItemSelectListener

/**
 * zhangjunfei  2020/11/12 16:12
 */
abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!getDarkModeStatus(this)) {
            initStatusBar()
        }
    }


    /**
     * 检查当前系统是否已开启暗黑模式
     */
    private fun getDarkModeStatus(context: Context): Boolean {
        val mode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
        return mode == Configuration.UI_MODE_NIGHT_YES
    }

    /**
     * 初始化顶部状态栏
     */

    protected open fun initStatusBar() {
        //默认顶部状态栏字体为黑色,如需改变颜色,子类覆写此方法,不使用super,从新调用setNativeStatusBar
        //这样是为了解决setNativeStatusBar方法重复调用的问题
        setNativeStatusBar(StateBarType.DARK)
    }

    /**
     * 状态栏(展示信号区域)状态
     */
    protected enum class StateBarType {
        LIGHT, DARK, TRAN
    }

    /**
     * 设置顶栏文字颜色
     */

    protected open fun setNativeStatusBar(type: StateBarType?) {
        val window = this.window
        val decor = window.decorView
        when (type) {
            StateBarType.DARK -> decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            StateBarType.LIGHT -> decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            StateBarType.TRAN -> {
                //https://www.jianshu.com/p/e89ee0a77bb5
                //View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN设置全屏，顶栏展示图片的时候会用到
                //decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                decor.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                window.statusBarColor = Color.TRANSPARENT
            }
        }
    }

    /**
     * 弹出吐司
     */
    protected fun showToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    /**
     * loading框
     */
    protected fun showLoading(msg: String) {
        WaitDialog.show(msg)
    }

    /**
     * 取消loading框
     */
    protected fun dismissLoading() {
        WaitDialog.dismiss()
    }

    /**
     * 提示框
     */
    protected fun showAlert(
        title: String = "提示",
        msg: String = "消息内容",
        okText: String = "确定",
        onOkClick: () -> Unit = {}
    ) {
        MessageDialog.show(title, msg, okText)
            .setOkButton { _, _ ->
                onOkClick()
                false
            }
    }

    /**
     * 底部弹框
     */
    protected fun <T : ViewDataBinding> showBottomView(
        @LayoutRes layoutId: Int,
        onBindLayout: (dialog: BottomDialog, binding: T) -> Unit
    ) {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(this),
            layoutId,
            null,
            false
        )
        val onBindView = object : OnBindView<BottomDialog>(binding.root) {
            override fun onBind(dialog: BottomDialog?, v: View?) {
                onBindLayout(dialog!!, binding as T)
            }

        }
        BottomDialog.show(onBindView)
    }

    /**
     * 底部选框，单选
     */
    protected fun showSingleSelectMenu(
        singleSelectMenuText: Array<String>,
        selectIndex: Int = 0,
        onOkClick: (index: Int) -> Unit
    ) {
        var selectMenuIndex = selectIndex
        BottomMenu.show(singleSelectMenuText)
            .setOnMenuItemClickListener(object : OnMenuItemSelectListener<BottomMenu?>() {
                override fun onOneItemSelect(
                    dialog: BottomMenu?,
                    text: CharSequence,
                    index: Int,
                    select: Boolean
                ) {
                    selectMenuIndex = index
                }
            })
            .setCancelButton(
                "确定"
            ) { _, _ ->
                onOkClick(selectMenuIndex)
                false
            }
            .selection = selectMenuIndex
    }

    /**
     * 底部选框，多选
     */
    protected fun showBottomMenu(
        multiSelectMenuText: Array<String>,
        onOkClick: (selects: IntArray) -> Unit
    ) {

        var selectMenuIndexArray: IntArray = intArrayOf()

        BottomMenu.show(multiSelectMenuText)
            .setOnMenuItemClickListener(object : OnMenuItemSelectListener<BottomMenu?>() {
                override fun onMultiItemSelect(
                    dialog: BottomMenu?,
                    text: Array<CharSequence>,
                    index: IntArray
                ) {
                    selectMenuIndexArray = index
                }
            })
            .setCancelButton(
                "确定"
            ) { _, _ ->
                onOkClick(selectMenuIndexArray)
                false
            }
            .setSelection(selectMenuIndexArray)
    }

    /**
     * 成功
     */
    protected fun toastSuc(msg: String) {
        TipDialog.show(msg, WaitDialog.TYPE.SUCCESS)
    }

    /**
     * 警告
     */
    protected fun toastWarning(msg: String) {
        TipDialog.show(msg, WaitDialog.TYPE.WARNING)
    }

    /**
     * 错误
     */
    protected fun toastError(msg: String) {
        TipDialog.show(msg, WaitDialog.TYPE.ERROR)
    }
}